package gui_layer;
import java.awt.Point;
import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import model_layer.Product;
import ctr_layer.ProductCtr;

@SuppressWarnings("serial")
public class ManageProductsGUI extends UniversalManageWindow
{
	private List<Product> all_products;
	private ProductCtr prod_ctr;
	protected String[] column_names = { "Art number", "Name", "Quantity", "Retail Price", "Profit %", "Sale Price" };
	
	public ManageProductsGUI()
	{
		prod_ctr = new ProductCtr();
		prepare_table(new Object[0][5], column_names);
		get_non_deleted_products();
		customize_table();
		customize_box();
		customize_search();
		this.setTitle("Manage Products");
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			@Override
			public void internalFrameActivated(InternalFrameEvent arg0)
			{
				dynamically_search();
				
			}
		});
	}
	
	private void customize_table()
	{
		table.getColumnModel().getColumn(2).setCellRenderer(new MarkingCellRenderer());
	}
	
	private void customize_box()
	{
		show_deleted_chckbox.setText("deleted products");
	}
	
	private void customize_search()
	{
		search_lbl.setText("Name/ID");
	}
	
	private void get_non_deleted_products()
	{
		all_products = prod_ctr.find_non_deleted_products();
	}
	
	private void get_deleted_products()
	{
		all_products = prod_ctr.find_deleted_products();
	}

	@Override
	protected void dynamically_search()
	{
		String text = search_field.getText().toLowerCase();
		MyTableModel mtm = (MyTableModel) table.getModel();
		Vector<Object[]> vector = new Vector<Object[]>();
		Object[] data;
		int next_row = 0;
		MarkingCellRenderer mcr = (MarkingCellRenderer) table.getColumnModel().getColumn(2).getCellRenderer();
		for(Product prod : all_products)
		{
			if(prod.getName().toLowerCase().startsWith(text) || prod.getId().startsWith(text))
			{
				
				data = new Object[6];
				data[0] = Integer.parseInt(prod.getId());
				data[1] = prod.getName();
				float quantity = prod.getQuantity();
				data[2] = quantity;
				
				if(quantity <= 0)
				{
					mcr.add_warning_cell(next_row);
					mcr.remove_notification_cell(next_row);
				}
				else if(quantity < prod.getMin_amount())
				{
					mcr.add_notification_cell(next_row);
					mcr.remove_warning_cell(next_row);
				}
				else
				{
					mcr.remove_notification_cell(next_row);
					mcr.remove_warning_cell(next_row);
				}
				next_row++;
				float last_retail_price = prod.getLast_retail_price();
				if(last_retail_price == 0)
				{
					data[3] = null;
					data[4] = null;
				}
				else
				{
					data[3] = last_retail_price;
					data[4] = (prod.getPrice() - last_retail_price)/last_retail_price * 100;
				}
				
				data[5] = prod.getPrice();
				vector.add(data);
			}
		}
		Object[][] filling = new Object[vector.size()][6];
		for(int i = 0; i < vector.size(); i++)
		{
			filling[i] = vector.get(i);
		}
		mtm.setDataVector(filling, column_names);
		table.getColumnModel().getColumn(2).setCellRenderer(mcr);
	}

	@Override
	protected void show_deleted()
	{
		get_deleted_products();
		dynamically_search();

	}
	
	@Override
	protected void show_non_deleted()
	{
		get_non_deleted_products();
		dynamically_search();
	}

	@Override
	protected void create_new()
	{
		NewProductGUI np_gui = new NewProductGUI(null, false);
		this.getDesktopPane().add(np_gui);
		Point window_corner = this.getLocation();
		np_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
		np_gui.setVisible(true);
		np_gui.addInternalFrameListener(new InternalFrameAdapter()
		{
			@Override
			public void internalFrameClosing(InternalFrameEvent arg0)
			{
				dynamically_search();
				
			}
		});

	}

	@Override
	protected void edit()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			String id = String.valueOf(table.getModel().getValueAt(row, 0));
			NewProductGUI ep_gui = new NewProductGUI(prod_ctr.find_product_by_id(id), true);
			this.getDesktopPane().add(ep_gui);
			Point window_corner = this.getLocation();
			ep_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
			ep_gui.setVisible(true);
			ep_gui.addInternalFrameListener(new InternalFrameAdapter()
			{
				@Override
				public void internalFrameClosing(InternalFrameEvent arg0)
				{
					dynamically_search();
					
				}
			});
		}
		

	}
	
	@Override
	protected void view()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			String id = String.valueOf(table.getModel().getValueAt(row, 0));
			NewProductGUI vp_gui = new NewProductGUI(prod_ctr.find_product_by_id(id), false);
			this.getDesktopPane().add(vp_gui);
			Point window_corner = this.getLocation();
			vp_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
			vp_gui.setVisible(true);
		}
	}

	@Override
	protected void delete()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			int id = (int) table.getModel().getValueAt(row, 0);
			if(((float) table.getModel().getValueAt(row, 2)) > 0)
			{
				if(JOptionPane.showConfirmDialog(this, "Product's quantity is higher than 0!\nAre you absolutely sure you wish to delete it?",
						"Delete product", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
				{
					if(prod_ctr.delete_product(id))
					{
						JOptionPane.showMessageDialog(this, "Product successfully deleted!", "Success", JOptionPane.PLAIN_MESSAGE);
						show_non_deleted();
					}
					else JOptionPane.showMessageDialog(this, "Error!\nProduct wasn't deleted", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
			else if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to delete this product? This can be reversed.",
					"Delete product", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(prod_ctr.delete_product(id))
				{
					JOptionPane.showMessageDialog(this, "Product successfully deleted!", "Success", JOptionPane.PLAIN_MESSAGE);
					show_non_deleted();
				}
				else JOptionPane.showMessageDialog(this, "Error!\nProduct wasn't deleted", "Error", JOptionPane.ERROR_MESSAGE);
			}
			
		}
	}
	
	@Override
	protected void restore()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			int id = (int) table.getModel().getValueAt(row, 0);
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to restore this product?",
					"Restore product", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(prod_ctr.restore_product(id))
				{
					JOptionPane.showMessageDialog(this, "Product successfully restored!", "Success", JOptionPane.PLAIN_MESSAGE);
					show_deleted();
				}
				else JOptionPane.showMessageDialog(this, "Error!\nProduct wasn't restored", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

}
